package com.cy.sys.log.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.sys.Aop.ancution.LoginObject;
import com.cy.sys.Aop.ancution.requestCache;
import com.cy.sys.log.Dao.SysConsumerDao;
import com.cy.sys.log.Dao.SysconumerRoleDao;
import com.cy.sys.log.pojo.pageObject;
import com.cy.sys.log.service.SysConumerSerivce;
import com.cy.sys.log.service.exception.ServiceException;
import com.cy.sys.pojo.SysConsumer;
import com.cy.sys.pojo.SysRoleConumer;
import com.cy.sys.pojo.SysUsersinsert;
import com.cy.sys.shiro.shirogetUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.micrometer.core.instrument.util.StringUtils;

@Service
public class SysConumerSerivceImpl implements SysConumerSerivce{
	@Autowired
	private SysConsumerDao sysconumersvs;
	@Autowired
	private SysconumerRoleDao sysuserroledao;
	
	//修改密码
	@Override
	public int updatenewpwd(String pwd, String newPwd, String cfgPwd) {
		//参数校验
		if(StringUtils.isEmpty(pwd))
			throw new IllegalArgumentException("原密码不能为空");
		if(StringUtils.isEmpty(newPwd))
			throw new IllegalArgumentException("新密码不能为空");
		if(!newPwd.equals(cfgPwd))
			throw new IllegalArgumentException("两次密码必须一致");
		
		//原密码与数据库密码校验
		SysUsersinsert sysuser = shirogetUtils.sysuserna();
		String salt = sysuser.getSalt();
		String saltpwd = new SimpleHash("MD5", pwd, salt, 1).toHex();
		if(!sysuser.getPassword().equals(saltpwd))
			throw new IllegalArgumentException("与原密码不一致");
		//修改密码
		String newsalt = UUID.randomUUID().toString();
		SimpleHash newpsw = new SimpleHash("MD5", newPwd, newsalt, 1);
		int ross = sysconumersvs.updatenewpwd(sysuser.getUsername(), newpsw.toHex(), newsalt);
		//校验数据并返回
		if(ross==0)
			throw new ServiceException("修改不成功或此用户不存在");
		return ross;
	}
	
	
	//角色部门
	@Override
	public List<SysRoleConumer> selectdept() {
		List<SysRoleConumer> DEPT = sysuserroledao.selectdept();
		if(DEPT==null)
			throw new ServiceException("对不起没有查询到信息");
		return DEPT;
	}
	
	@LoginObject("修改保存")
	@Override
	public int UpdateSaveObject(SysUsersinsert sysusers, Integer[] roleIds) {
			// 1.校验数据
			if(sysusers==null||roleIds==null||roleIds.length==0)
				throw new IllegalArgumentException("无效的参数");
			//更新自身信息
			int into = sysconumersvs.updateUsersById(sysusers);
			if(into==0)
				throw new ServiceException("更新有误");
			//删除原有关系数据
			sysuserroledao.deleteUserRoleById(sysusers.getId());		
			//新增角色关系数据
			sysuserroledao.usersRole(sysusers.getId(), roleIds);
			return into;
	}
	
	/**新增
	 * 1.校验数据
	 * 2.保存用户自身信息
	 * 2.1构建颜值对象
	 * 2.2对密码进行颜值加密
	 * 2.2.1加密方式一(借助spring框架工具类DigestUtils)
	 * 2.2.2加密方式二(借助shiro框架API第三方框架)
	 */
	@Override
	@LoginObject("新增用户")
	public int userinsertObject(SysUsersinsert sysusers, Integer[] roleIds) {
		// 1.校验数据
		if(sysusers==null||roleIds==null||roleIds.length==0)
			throw new IllegalArgumentException("无效的参数");
		//保存用户自身信息对密码进行加密
		//盐值算法
		String pas = sysusers.getPassword();
		String salt = UUID.randomUUID().toString();
		//使用shiro进行加密
		SimpleHash newsimpleHash = new SimpleHash("Md5", pas, salt, 1);		
		String newpas = newsimpleHash.toHex();
		sysusers.setSalt(salt);
		sysusers.setPassword(newpas);
		//新增信息
		int into = sysconumersvs.insertfindById(sysusers);
		if(into==0)
			throw new ServiceException("新增有误");
		//新增角色关系数据
		sysuserroledao.usersRole(sysusers.getId(), roleIds);
		return into;
	}
	
	
	//修改
	@Override
	public Map<String, Object> selectfindById(Integer id) {
		//参数校验
		if(id<1||id==null)
			throw new IllegalArgumentException("参数无效");
		SysConsumer sysconumer = sysconumersvs.selectfindById(id);
		if(sysconumer==null)
			throw new ServiceException("对不起查询为空");
		//查询角色关系网
		List<Integer> rolesid = sysuserroledao.selectconumerRole(id);
		
		 Map<String, Object> map = new HashMap<>();
		 map.put("user", sysconumer);
		 map.put("roleIds",rolesid);
		return map;
	}
	/**
	 * @RequiresPermissions 注解描述的方法为一个权限控制切入点方法(目标方法),当我们访问此方法时需要授权才能访问?
	 * FAQ:
	 * 1)什么情况下会进行授权?(当用户权限中包含@RequiresPermissions注解内部定义的字符串)
	 * 2)如何获取登录用户的权限标识?(基于登陆用户信息访问用户角色,角色菜单,菜单表中的数据)
	 * 3)由谁授权?(SecurityManager)
	 * 4)在什么样的方法中进行授权?(AOP编程过程中的通知方法中.)
	 * 5)假如用户没有访问此方法的权限怎么办?抛出异常
	 */
	
	@Override
	@LoginObject("启用禁用")
	@RequiresPermissions("sys:user:update")
	@Transactional(rollbackFor = Throwable.class)//spring事务的回滚与提交
	public int updatestateById(Integer id, Integer valid) {
		//参数校验
		if(id==null||valid==null)
			throw new IllegalArgumentException("参数不合法");
		int row = sysconumersvs.updatestateById(id, valid);
		if(row==0)
			throw new ServiceException("异常");
		return row;
	}
	
	
	@Override
	@LoginObject("分页查询保存")
	@Transactional(readOnly = true) //查询事务用readOnly = true 性能较高
	public pageObject<SysConsumer> findAllUserconsumer(String username, Integer pageCurrent) {
		System.out.println("-----------");
		//判断处理
		if(pageCurrent==null||pageCurrent<0)
			throw new IllegalArgumentException("参数异常不合法");
		
		int pageSize = 3;
		Page<SysConsumer> page=PageHelper.startPage(pageCurrent, pageSize);
		
		List<SysConsumer> recosd=sysconumersvs.findAllUserconsumer(username);
		//4.对查询结果进行封装并返回。
		//return new PageObject<>((int)page.getTotal(), pageSize, pageCurrent, records);
	    return new pageObject<>(page.getPages(), pageSize, (int)page.getTotal(), pageCurrent, recosd);
	}
	
	

}
